package com.jvtd.rong_im.listener;

import com.jvtd.rong_im.mInterface.RongIMTypingStatusCallback;

import java.util.Collection;
import java.util.Iterator;

import io.rong.imlib.MessageTag;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.TypingMessage.TypingStatus;
import io.rong.imlib.model.Conversation;
import io.rong.message.TextMessage;
import io.rong.message.VoiceMessage;

/*
 * Create by Jack Zhang
 */
public class BaseTypingStatusListener implements RongIMClient.TypingStatusListener
{
  private String mTitle;
  private String mTargetId;
  private Conversation.ConversationType mConversationType;
  private RongIMTypingStatusCallback callback;

  public BaseTypingStatusListener(String title, String targetId, Conversation.ConversationType conversationType, RongIMTypingStatusCallback callback)
  {
    this.mTitle = title;
    this.mTargetId = targetId;
    this.mConversationType = conversationType;
    this.callback = callback;
  }

  @Override
  public void onTypingStatusChanged(Conversation.ConversationType conversationType, String targetId, Collection<TypingStatus> collection)
  {
    // 当输入状态的会话类型和targetID与当前会话一致时，才需要显示
    if (conversationType.equals(mConversationType) && targetId.equals(mTargetId) && callback != null)
    {
      //count表示当前会话中正在输入的用户数量，目前只支持单聊，所以判断大于0就可以给予显示了
      int count = collection.size();
      if (count > 0)
      {
        Iterator iterator = collection.iterator();
        TypingStatus status = (TypingStatus) iterator.next();
        String objectName = status.getTypingContentType();

        MessageTag textTag = TextMessage.class.getAnnotation(MessageTag.class);
        MessageTag voiceTag = VoiceMessage.class.getAnnotation(MessageTag.class);
        //匹配对方正在输入的是文本消息还是语音消息
        if (objectName.equals(textTag != null ? textTag.value() : null))// 显示“对方正在输入”
          callback.setTitleContent("对方正在输入");
        else if (objectName.equals(voiceTag != null ? voiceTag.value() : null))// 显示"对方正在讲话"
          callback.setTitleContent("对方正在讲话");
      } else// 当前会话没有用户正在输入，标题栏仍显示原来标题
        callback.setTitleContent(mTitle);
    }
  }
}
